import 'package:flutter/material.dart';

class Button extends StatefulWidget {
  final String data;
  final Color background;
  final Color highlightBackground;
  final Color textColor;
  final Color highlightTextColor;
  final double radius;
  final double textSize;
  final Decoration decoration;
  final EdgeInsetsGeometry padding;
  final TextStyle style;
  final BorderSide borderSide;
  final VoidCallback onPressed;

  const Button(this.data,
      {Key key,
        @required this.onPressed,
        this.padding,
        this.background,
        this.highlightBackground,
        this.textColor = Colors.white,
        this.highlightTextColor = Colors.white,
        this.radius = 6.0,
        this.decoration,
        this.style,
        this.borderSide,
        this.textSize = 18.0})
      : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _ButtonState();
  }
}

class _ButtonState extends State<Button> {
  Color textColor; //=  widget.defaultColor;
  bool pressed = false;
  Color background;
  Color highlightBackground;

  void updateColor() {
    setState(() {
      textColor = pressed ? widget.highlightTextColor : widget.textColor;
    });
  }

  @override
  void initState() {
    background =
    null == widget.background ? Color(0XFF2864A7) : widget.background;
    highlightBackground = null == widget.highlightBackground
        ? Color(0xFF0777CD)
        : widget.highlightBackground;
    super.initState();
    updateColor();
  }

  @override
  Widget build(BuildContext context) {
    double radius = null == widget.radius ? 8.0 : widget.radius;
    BorderSide borderSide =
    null == widget.borderSide ? BorderSide.none : widget.borderSide;
    EdgeInsetsGeometry padding =
    null == widget.padding ? EdgeInsets.all(8.0) : widget.padding;

    return RaisedButton(
      onPressed: widget.onPressed,
      highlightColor: highlightBackground,
      color: background,
      shape: RoundedRectangleBorder(
          side: borderSide,
          borderRadius: BorderRadius.all(Radius.circular(radius))),
      textColor: textColor,
      padding: padding,
      onHighlightChanged: (bool changed) {
        this.pressed = changed;
        updateColor();
      },
      child: Text(
        widget.data,
        textAlign: TextAlign.center,
        style: null == widget.style
            ? TextStyle(fontSize: widget.textSize)
            : widget.style,
      ),
    );
  }
}
//Expanded 充满布局
class ExpandedButton extends StatelessWidget {
  final String data;
  final Color background;

  final Color highlightBackground;

  final Color textColor;
  final Color highlightTextColor;
  final double radius;
  final double textSize;
  final Decoration decoration;
  final EdgeInsetsGeometry padding;
  final EdgeInsetsGeometry margin;
  final TextStyle style;
  final VoidCallback onPressed;

  ExpandedButton(
      this.data, {
        Key key,
        this.onPressed,
        this.background,
        this.highlightBackground,
        this.textColor,
        this.highlightTextColor,
        this.radius,
        this.textSize,
        this.decoration,
        this.padding,
        this.margin,
        this.style,
      });

  @override
  Widget build(BuildContext context) {
    TextStyle textStyle = null == style ? TextStyle(fontSize: 20) : style;
    return Expanded(
        child: Container(
            margin: margin,
            child: Button(
              data,
              onPressed: onPressed,
              padding: padding,
              radius: radius,
              background: background,
              highlightBackground: highlightBackground,
              style: textStyle,
              textColor: textColor,
              highlightTextColor: highlightTextColor,
            )));
  }
}

class ButtonText extends StatefulWidget {
  final String data;
  final Color background;
  final Color highlightBackground;
  final Color textColor;
  final Color highlightTextColor;
  final double textSize;
  final EdgeInsetsGeometry padding;
  final TextStyle style;
  final TextAlign textAlign;
  final VoidCallback onPressed;
  final FontWeight fontWeight;

  ButtonText(
      this.data, {
        this.onPressed,
        this.padding,
        this.highlightBackground,
        this.textColor,
        this.highlightTextColor,
        this.textSize,
        this.textAlign,
        this.fontWeight,
        this.style,
        this.background,
      });

  @override
  State<StatefulWidget> createState() {
    return _ButtonTextState();
  }
}

class _ButtonTextState extends State<ButtonText> {
  Color textColor; //=  widget.defaultColor;
  bool pressed = false;
  Color background;
  Color highlightBackground;

  void updateColor() {
    setState(() {
      textColor = pressed ? widget.highlightTextColor : widget.textColor;
    });
  }

  @override
  void initState() {
    background =
    null == widget.background ? Colors.transparent : widget.background;
    highlightBackground = null == widget.highlightBackground
        ? Colors.transparent
        : widget.highlightBackground;
    super.initState();
    updateColor();
  }

  @override
  Widget build(BuildContext context) {
    return FlatButton(
      shape: null,
      onPressed: widget.onPressed,
      highlightColor: highlightBackground,
      color: background,
      textColor: textColor,
      padding: null == widget.padding ? EdgeInsets.all(8.0) : widget.padding,
      onHighlightChanged: (bool changed) {
        this.pressed = changed;
        updateColor();
      },
      child: Text(
        widget.data,
        textAlign:
        null == widget.textAlign ? TextAlign.start : widget.textAlign,
        style: null == widget.style
            ? TextStyle(
            color: textColor,
            fontSize: widget.textSize,
            fontWeight: widget.fontWeight)
            : widget.style,
      ),
    );
  }
}